﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Web.DatabaseAccess
{
    public static class DatabaseHelper
    {
        public static DataSet ExecuteStoredProcedure(string procedureName, params object[] parameterValues)
        {
            DataSet ds = null;
            try
            {
                string connectToSQL = "Data Source=localhost;Initial Catalog=Firebird;Integrated Security=True";
                using (SqlConnection con = new SqlConnection(connectToSQL))
                {
                    con.Open();
                    using (SqlCommand command = new SqlCommand("", con))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        SqlCommandBuilder.DeriveParameters(command);
                        command.CommandText = procedureName;
                        //command.Parameters.Add("@int", SqlDbType.Int);
                        command.Parameters.RemoveAt(0);
                        for (int i = 0; i < parameterValues.Length; i++)
                            command.Parameters[i].Value = parameterValues[i];
                        using (SqlDataAdapter reader = new SqlDataAdapter(command))
                        {
                            ds = new DataSet();
                            reader.Fill(ds);
                        }
                    }
                }

            }
            catch (InvalidOperationException ioe)
            {
                return null;
            }
            return ds;
        }

        public static int SQLConnect_ExecuteNonQuery(string spName, params object[] parameterValues)
        {
            //Data Source=ip;Initial Catalog=dbname;
//            string connectToSQL = "Data Source=localhost;Initial Catalog=Firebird;Integrated Security=True";
            string connectToSQL = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;

            using (SqlConnection con = new SqlConnection(connectToSQL))
            {
                con.Open();
                using (SqlCommand command = new SqlCommand(spName, con))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    SqlCommandBuilder.DeriveParameters(command);
                    command.CommandText = spName;
                    //command.Parameters.Add("@int", SqlDbType.Int);
                    foreach (SqlParameter p in command.Parameters)
                    {
                        Console.WriteLine(p.ParameterName);
                    }
                    command.Parameters.RemoveAt(0);
                    for (int i = 0; i < parameterValues.Length; i++)
                        command.Parameters[i].Value = parameterValues[i];

                    command.ExecuteNonQuery();
                    con.Close();
                    return 0;
                }
            }
        }
    }
}
